<template>
	<view>
		<view style="line-height: 100rpx;display: flex;position: fixed;top: 0;left: 0;width: 100%;">
			<view style="flex: 1;font-size: 30rpx;text-align: center;" @click="dibu(1)" :class="type==1?'active':'as'">
				私信
			</view>
			<view style="flex: 1;font-size: 30rpx;text-align: center;" @click="dibu(2)" :class="type==2?'active':'as'">
				群组
			</view>
		</view>
		<view style="" v-if="type==1" style="width: 92%;margin: 0 auto;">
			<view @tap="handleRoute(item,index)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;"
				v-for="(item,index) in list" :key='index'>
				<view style="float: left;width: 19%;overflow: hidden;">
					<image :src="item.client.avatar"
						style="width: 100rpx;height: 100rpx;border-radius: 50%;display: block;float: left;margin-top: 6rpx;"
						mode=""></image>
				</view>
				<view
					style="width: 81%;float: left;padding: 10rpx 0 10rpx 0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
					<view style="overflow: hidden;">
						<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;width: 50%;float: left;"
							class="yihang">
							{{item.client.remark_name || item.client.name || item.client.nickname || ''}}
						</view>
						<view style="color: #666;font-size: 26rpx;line-height: 50rpx;width: 50%;float: left;text-align: right;"
							class="yihang">
							{{item.create_time_text || ''}}
						</view>
					</view>
					<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;">
						<view v-if="item.content_type=='TIMTextElem'" class="yihang">
							{{item.content || ''}}
						</view>
						<view v-if="item.content_type=='TIMImageElem'" class="yihang">
							[图片]
						</view>
						<view v-if="item.content_type=='TIMVideoFileElem'" class="yihang">
							[视频]
						</view>
						<view v-if="item.content_type=='TIMSoundElem'" class="yihang">
							[语音]
						</view>
					</view>
				</view>
			</view>
		</view>
		<view v-if="type==2" style="width: 92%;margin: 0 auto;">
			<view @tap="handleRoute(item,index)" style="overflow: hidden;margin-bottom: 30rpx;position: relative;"
				v-for="(item,index) in list" :key='index'>
				<view style="float: left;width: 19%;overflow: hidden;">
					<image :src="item.group.avatar"
						style="width: 100rpx;height: 100rpx;border-radius: 50%;display: block;float: left;margin-top: 6rpx;"
						mode=""></image>
				</view>
				<view
					style="width: 81%;float: left;padding: 10rpx 100rpx 10rpx 0;box-sizing: border-box;border-bottom: 1px solid #f5f5f5;">
					<view style="overflow: hidden;">
						<view style="color: #000000;font-size: 30rpx;font-weight: 600;line-height: 50rpx;width: 100%;float: left;"
							class="yihang">
							{{item.group.name || ''}}
						</view>
						<!-- <view style="color: #666;font-size: 26rpx;line-height: 50rpx;width: 50%;float: left;text-align: right;"
							class="yihang">
							{{item.create_time_text || ''}}
						</view> -->
					</view>
					<view style="color: #999999;font-size: 24rpx;line-height: 50rpx;">
						<view v-if="item.content_type=='TIMTextElem'" class="yihang">
							{{item.content || ''}}
						</view>
						<view v-if="item.content_type=='TIMImageElem'" class="yihang">
							[图片]
						</view>
						<view v-if="item.content_type=='TIMVideoFileElem'" class="yihang">
							[视频]
						</view>
						<view v-if="item.content_type=='TIMSoundElem'" class="yihang">
							[语音]
						</view>
					</view>
				</view>
				<image @click.stop="qunxiaoxi(item.group_id,2)"
					src="https://static-1310167831.cos.ap-nanjing.myqcloud.com/uploads/20230510/599ed2f6afe936c26883d8be1779bb11.png"
					style="width: 50rpx;height: 50rpx;display: block;position: absolute;top: 50%;right: 30rpx;transform: translateY(-50%);"
					mode=""></image>
			</view>
		</view>
		<view style="text-align: center;margin-top: 20rpx;">
			<image v-if="list.length==0"
				:src="require('@/static/my/no-data.png')"
				style="width: 334rpx;" mode="widthFix"></image>
		</view>

	</view>
</template>

<script>
	import isAuditIos from '@/common/isAuditIos.js'
	export default{
		mixins: [isAuditIos],
		data(){
			return{
				type:1,
				list:[]
			}
		},
		onLoad() {
			this.pengyou()
		},
		methods:{
			dibu:function(index){
				this.type=index
				if(index==1){
					this.pengyou()
				}else{
					this.qun()
				}
			},
			qunxiaoxi:function(id,index){
				let that = this
				that.post1('api/im/im/check_group',{group_id:id},false).then(re=>{
					if(re.code==1){
						uni.navigateTo({
							url: '/sub/pages/qun_xiaoxi?id=' + id
						})
					}else if(re.code==211){
						if (that.iosUpAppStore) {
							uni.showModal({
								title: '提示',
								content: re.msg,
								success: function(res1) {
									if (res1.confirm) {
										uni.navigateTo({
											url: '../../subpages/pages/vip'
										})
									} else if (res1.cancel) {

									}
								}
							});
						} else {
							uni.navigateTo({
								url: '/sub/pages/qun_xiaoxi?id=' + id
							})
						}
					}else{
						uni.showToast({
							title: re.msg,
							icon: "none",
							duration: 1000
						})
					}
				})
			},

			pengyou:function(){
				this.post('api/im/im/messagef_list','',true).then(res=>{
					console.log(res)
					this.list=res.data
				})
			},
			qun:function(){
				this.post('api/im/im/message_list','',true).then(res=>{
					console.log(res)
					this.list=res.data
				})
			},


			handleRoute(item, index) {
				let that = this
				if (item.type === 'C2C') {
					let aid = 'C2C' + item.client.mobile2
					this.post1('api/client/info', '',false).then(res => {
						console.log(res)
						if (res.code == 1) {
							that.post1('api/im/im/friend_msg', {
								client_id: 0,
								client_mobile: item.client.mobile2
							},false).then(res => {
								console.log(1111111111)
								if (res.code == 1) {
									uni.setStorageSync('friend_id', res.data.friend_id)
									// uni.setStorageSync('friend_name',id)
									uni.navigateTo({
										url: `/im/chat/chat?conversationID=${aid}`,
									});
								}else if(res.code==211){
									if (that.iosUpAppStore) {
										uni.showModal({
											title: '提示',
											content: res.msg,
											success: function (res1) {
												if (res1.confirm) {
													if (that.iosUpAppStore) {
														uni.navigateTo({
															url:'/subpages/pages/vip'
														})
													}
												} else if (res1.cancel) {

												}
											}
										});
									}

								}
								else {
									uni.showToast({
										title: res.msg,
										icon: "none",
										duration: 1000
									})
								}
							})
							// return
							// if (res.data.is_vip == 1) {

							// } else if (res.data.is_vip == 2) {
							// 	uni.showModal({
							// 		title: '提示',
							// 		content: '您的会员已到期,只有会员才可以聊天,请前去续费会员！',
							// 		success: function(res1) {
							// 			if (res1.confirm) {
							// 				uni.navigateTo({
							// 					url: '../../subpages/pages/vip'
							// 				})
							// 			} else if (res1.cancel) {

							// 			}
							// 		}
							// 	});
							// } else if (res.data.is_vip == 0) {
							// 	uni.showModal({
							// 		title: '提示',
							// 		content: '只有会员才可以聊天,请前去开通会员！',
							// 		success: function(res1) {
							// 			if (res1.confirm) {
							// 				uni.navigateTo({
							// 					url: '../../subpages/pages/vip'
							// 				})
							// 			} else if (res1.cancel) {

							// 			}
							// 		}
							// 	});
							// }
						}
					})
				} else {
					let aid = 'GROUP'+item.group_id
					if(item.type=='ChatRoom'){
						this.post1('api/im/im/purchasing', {
							group_id:item.group_id
						},false).then(res => {
							console.log(res)

							if (res.code == 1) {
								if (uni.getStorageSync('userinfo').is_server == 1) {
									uni.setStorageSync('user_id', res.data.user.id)
									uni.setStorageSync('friend_name', res.data.user.remark_name || res.data.user.name)
								} else {
									uni.setStorageSync('user_id', 0)
									uni.setStorageSync('friend_name', res.data.server.name)
								}

								uni.navigateTo({
									url: `/im/chat/dan_chat?conversationID=${aid}`,
									// url: `/im/login/login`,
								})
							} else {
								uni.showToast({
									title: res.msg,
									icon: "none",
									duration: 1000
								})
							}
						})
						return
					}else{

						this.post1('api/client/info', '',false).then(res => {
							console.log(res)
							if (res.code == 1) {
								that.post1('api/im/im/check_group',{group_id:item.group_id},false).then(re=>{
									if(re.code==1){
										uni.setStorageSync('group_id', item.group_id)
										uni.navigateTo({
											url: `/im/chat/group_chat?conversationID=${aid}`,
											// url: `/im/login/login`,
										})
									}else if(re.code==211){
										if (that.iosUpAppStore) {
											uni.showModal({
												title: '提示',
												content: re.msg,
												success: function(res1) {
													if (res1.confirm) {
														if (that.iosUpAppStore) {
															uni.navigateTo({
																url: '../../subpages/pages/vip'
															})
														}
													} else if (res1.cancel) {

													}
												}
											});
										}
									}else{
										uni.showToast({
											title: re.msg,
											icon: "none",
											duration: 1000
										})
									}
								})
								return
								if (res.data.is_vip == 1) {
									uni.setStorageSync('group_id', that.conversationList[index].conversationID.substr(
										5))
									uni.navigateTo({
										url: `/im/chat/group_chat?conversationID=${aid}`,
										// url: `/im/login/login`,
									})
								} else if (res.data.is_vip == 2) {
									uni.showModal({
										title: '提示',
										content: '您的会员已到期,只有会员才可以聊天,请前去续费会员！',
										success: function(res1) {
											if (res1.confirm) {
												uni.navigateTo({
													url: '../../subpages/pages/vip'
												})
											} else if (res1.cancel) {

											}
										}
									});
								} else if (res.data.is_vip == 0) {
									uni.showModal({
										title: '提示',
										content: '只有会员才可以聊天,请前去开通会员！',
										success: function(res1) {
											if (res1.confirm) {
												uni.navigateTo({
													url: '../../subpages/pages/vip'
												})
											} else if (res1.cancel) {

											}
										}
									});
								}
							}
						})
					}
					console.log(this.conversationList, 747777)

				}



			},


		}
	}
</script>

<style lang="scss">
	page{
		background-color: #fff;
		padding-top: 100rpx;
		padding-bottom: 30rpx;
	}
	.active{
		color: #1fb0ac;
		position: relative;
	}
	.as{
		color: #666666;
	}
	.active::after {
		content: ''; // 必须
		position: absolute;
		left: 50%;
		bottom: 20rpx;
		transform: translateX(-50%);
		width: 60rpx;
		height: 6rpx;
		background-color: #1fb0ac;
	}
</style>